package com.jit.BluetoothKey.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.jit.BluetoothKey.R;
import com.jit.BluetoothKey.activity.AboutActivity;
import com.jit.BluetoothKey.activity.BluetoothConnectionActivity;
import com.jit.BluetoothKey.fragment.base.BaseFragment;
import com.jit.BluetoothKey.utils.SharedPreferencesUtil;
import com.jit.BluetoothKey.utils.UserFiled;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by Administrator on 2018/1/25.
 * 设置Fragment
 */

public class SettingFragment extends BaseFragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

    private RelativeLayout bluetoothConnectionRl;
    private TextView bleName;
    private Switch bleConnectionSwich;
    private Switch userLoginSwich;
    private RelativeLayout aboutRl;

    @Override
    protected int getLayoutId() {
        return R.layout.fragent_setting;
    }

    @Override
    protected void initView() {
        super.initView();
        bluetoothConnectionRl = mRootView.findViewById(R.id.setting_bluetooth_connection_rl);
        bleName = mRootView.findViewById(R.id.fragment_ble_name);
        bleConnectionSwich = mRootView.findViewById(R.id.setting_bleconnection_swb);
        userLoginSwich = mRootView.findViewById(R.id.setting_login_swb);
        aboutRl = mRootView.findViewById(R.id.fragment_setting_abuot);
    }

    @Override
    protected void initData() {
        super.initData();
        bleName();  //渲染蓝牙名称
        bleConnectionSwich.setChecked(SharedPreferencesUtil.getBoolean(mActivity, UserFiled.BLE_AUTO_CONNECTION));
        userLoginSwich.setChecked(SharedPreferencesUtil.getBoolean(mActivity, UserFiled.BLE_AUTO_LOGIN));
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setBleName(String event) {
        if (event.equals(UserFiled.BLE_CHANGE)) {
            bleName();
        }
    }

    @Override
    protected void initListner() {
        super.initListner();
        bluetoothConnectionRl.setOnClickListener(this);
        bleConnectionSwich.setOnCheckedChangeListener(this);
        userLoginSwich.setOnCheckedChangeListener(this);
        aboutRl.setOnClickListener(this);
    }

    /**
     * 渲染本地存储的蓝牙名称
     */
    private void bleName() {
        bleName.setText(SharedPreferencesUtil.getString(mActivity, UserFiled.BINGDING_BLUETOOTH_NAME));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.setting_bluetooth_connection_rl:
                startActivity(new Intent(mActivity, BluetoothConnectionActivity.class));
                break;
            case R.id.fragment_setting_abuot:
                startActivity(new Intent(mActivity, AboutActivity.class));
                break;
        }
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.setting_bleconnection_swb:
                SharedPreferencesUtil.setBoolean(mActivity, UserFiled.BLE_AUTO_CONNECTION, isChecked);
                break;
            case R.id.setting_login_swb:
                SharedPreferencesUtil.setBoolean(mActivity, UserFiled.BLE_AUTO_LOGIN, isChecked);
                break;
        }
    }
}
